package com.qkm.xinguan.controller;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePrecreateRequest;
import com.alipay.api.request.AlipayTradeQueryRequest;
import com.alipay.api.response.AlipayTradePrecreateResponse;
import com.alipay.api.response.AlipayTradeQueryResponse;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qkm.xinguan.domain.entity.Trade;
import com.qkm.xinguan.domain.transform.dto.TradeDTO;
import com.qkm.xinguan.exception.BindingResultException;
import com.qkm.xinguan.exception.BusinessException;
import com.qkm.xinguan.repository.TradeRepository;
import com.qkm.xinguan.response.Result;
import com.qkm.xinguan.response.ResultCode;
import com.qkm.xinguan.vo.HouseInfoVo;
import com.qkm.xinguan.vo.TradeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author zfj
 * @date 2021/12/27 -14:02
 */
@RestController
public class TradeController implements ITradeController {

	private final TradeRepository tradeRepository;

	@Autowired
	public TradeController(TradeRepository tradeRepository) {
		this.tradeRepository = tradeRepository;
	}

	@Override
	public String qrCode(TradeDTO tradeDTO, BindingResult result) throws AlipayApiException {
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do","2021003108655493",
				"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCIJ55N7tbDuees1cbfUfpEoPMulitFWDfjBlrkFva2oRsTvPmZnFxhZ7DcT9jkmhB+wQSoD+G/vjrUenxjZvV+fVkOEh+go9Zwil79xVDRxojtGa/5foTo58gu6TKyvMBx1p/MPlgXWyAzFypEX6ZM/ShsmAIYTcTSZG9S4H4ofd4O6BuPhJzg20u9AHBcBaM1bglqRXrCVLq1+bFES2QpXN+0QRYpR26gatBnMkVnnKHNPRzRacWkTWYHnMK9Y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ajDo3qlTcnB6lJy5hJd7ck9jRw6Qd1rzubIx7wcKEce3ivCNXw5vIiArmvwO3fpwSJubF0CgYEAkiMjcMnjEmGoybosv9WjI3kivRR8oP/Iau+xKN1Cvohx1iwRwSn7fkOGEzxrKk/ssvSHrvxSpzPdCvxHXZF8cbuPd8ozVkhZnYHX2WBY2LKI84saYClGSKMpPtWhg/EbOzvdJCzyf93+gqIpFfKUJFr8a2cvjor4Ol9BAdFcxAA=","json","UTF-8","MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs4BxSOViCI0gzl+IZ26IuxaIzW1JMaZ6vY8sJVx6A+iTN3WBj3Rn4oolUtZeQi0vI3SVZdZhwtKqZ9iam34IvkuvuxEcEuMxoLLFP51mY1dpY1hXwdMOmW6gwU4D6JWIZR0kyssbUav4mAsJyVt0t+T9rGS+xgeN5DRXs4qhfgbRGwqOmEuyJiwzXO3m40NaCeRZx8DKbZiGz3HadnGCnpcAT8Wko7gMPJ6l7tD/vPR5C5aLXGnx1zZgw4i3ig4IQdQrz3aSzZ6Hc+8aRnZ+pqX58NwJa+gG8tLS8j5MpKyN6GHux+KW5EZwUAZC9WLRp30dtVv5S86n/aHJa4NIOwIDAQAB","RSA2");
		AlipayTradePrecreateRequest request = new AlipayTradePrecreateRequest();
		request.setNotifyUrl("");
		JSONObject bizContent = new JSONObject();
		bizContent.put("out_trade_no", tradeDTO.getOutTradeNo());
		bizContent.put("total_amount", tradeDTO.getTotalAmount());
		bizContent.put("subject", tradeDTO.getSubject());
		request.setBizContent(bizContent.toString());
		AlipayTradePrecreateResponse response = alipayClient.execute(request);
		if(response.isSuccess()){
			return  response.getBody();
		} else {
			throw new BusinessException(ResultCode.ADD_ACCOUNT.getCode(), "二维码生成失败失败");
		}
	}

	@Override
	public String PayCheck(String id) throws AlipayApiException {
		//获得初始化的AlipayClient
		AlipayClient alipayClient = new DefaultAlipayClient("https://openapi.alipay.com/gateway.do", "2021003108655493", "MIIEvQIBADANBgk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wKBgAtOOSs15BbXRr9MOTg+YT8UVZdE0kbvGPSZO7pqRrBhIlNKRLvmsaSNE4U1lZGdWRyLKuMK6oSZ2cNw6VdJfBxVOqxRvcO1iyOKv7On0RBiaVxtV7IhkBeFapZjsSz+Fx48FQryxHEn0vtvUeYX8LfLVRjRifacEc+T9MeiRpLBAoGAZqD5olEQA5HRKkghIhqt2Gg6ZP9bnphxyzxCG7cVs+eLe3P/sv2tIPaAL4gWIGQD3kKXPNe/azwZ5ffQgCOm97B8WXWlzqU8vFSfPajDo3qlTcnB6lJy5hJd7ck9jRw6Qd1rzubIx7wcKEce3ivCNXw5vIiArmvwO3fpwSJubF0CgYEAkiMjcMnjEmGoybosv9WjI3kivRR8oP/Iau+xKN1Cvohx1iwRwSn7fkOGEzxrKk/ssvSHrvxSpzPdCvxHXZF8cbuPd8ozVkhZnYHX2WBY2LKI84saYClGSKMpPtWhg/EbOzvdJCzyf93+gqIpFfKUJFr8a2cvjor4Ol9BAdFcxAA=", "json", "UTF-8", "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAs4BxSOViCI0gzl+IZ26IuxaIzW1JMaZ6vY8sJVx6A+iTN3WBj3Rn4oolUtZeQi0vI3SVZdZhwtKqZ9iam34IvkuvuxEcEuMxoLLFP51mY1dpY1hXwdMOmW6gwU4D6JWIZR0kyssbUav4mAsJyVt0t+T9rGS+xgeN5DRXs4qhfgbRGwqOmEuyJiwzXO3m40NaCeRZx8DKbZiGz3HadnGCnpcAT8Wko7gMPJ6l7tD/vPR5C5aLXGnx1zZgw4i3ig4IQdQrz3aSzZ6Hc+8aRnZ+pqX58NwJa+gG8tLS8j5MpKyN6GHux+KW5EZwUAZC9WLRp30dtVv5S86n/aHJa4NIOwIDAQAB", "RSA2");
		//创建API对应的request类
		AlipayTradeQueryRequest request = new AlipayTradeQueryRequest();
		request.setBizContent("{" +
				"\"out_trade_no\":\""+id+"\"}");  //设置业务参数
		//通过alipayClient调用API，获得对应的response类
		AlipayTradeQueryResponse response = alipayClient.execute(request);
		return response.getBody();
	}

	@Override
	public Result addTrade(Trade trade, BindingResult result) {
		if (result.hasErrors()) {
			throw new BindingResultException(result);
		}
		boolean save=tradeRepository.save(trade);
		if (save) {
			return Result.ok().message("支付订单添加");
		} else {
			throw new BusinessException(ResultCode.ORDER_ADD);
		}
	}

	@Override
	public Result getTradePage(Long touristId) {
		return tradeRepository.getAllOrders(touristId);
	}

	@Override
	public Result getTradeList(Integer page, Integer size) {
		Page<Trade> pageInfo = tradeRepository.getTradeListPage(page, size);
		return Result.ok().data(pageInfo);
	}


}
